package jml.defaultimple.util;

import java.util.ArrayList;
import java.util.StringTokenizer;


public class UString
{
	public static String removeLeft(String s,String toRmv)
	{
		return s.substring(toRmv.length()+1);
	}
	
	public static String removeRight(String s,String toRmv)
	{
		return s.substring(0,s.lastIndexOf(toRmv));
	}
	
	public static String[] split(String s,String sep)
	{
		StringBuffer buf=new StringBuffer();
		StringTokenizer st=new StringTokenizer(s,sep);
		
	
		ArrayList<String> arrList=new ArrayList<String>();
		while( st.hasMoreTokens() )
		{
			arrList.add(st.nextToken());
		}
		
		String ret[]=new String[arrList.size()];
		return (String[])arrList.toArray(ret);
	}
	
	public static String replace(String s, String old, String sNew)
	{
		int p = s.indexOf(old);
		if (p < 0) return s;
		int len = old.length();
		String ret = s.substring(0, p) + sNew + s.substring(p + len);
		return replace(ret, old, sNew);
	}

	public static String nullIfEmpty(String s)
	{
		return s!=null&&s.length()==0?null:s;
	}
	
	public static String emtyIfNull(String s)
	{
		return s == null ? "" : s;
	}
	
	public static boolean isNullOrEmpty(String s)
	{
		return s==null?true:s.length()==0;
	}
	
	public static String join(String s[], char sep)
	{
		String aux="";
		for(int i=0; i<s.length; i++)
		{
			aux+=s[i]+(i<s.length-1?", ":"");
		}
		
		return aux;
	}

	public static String trim(String s)
	{
		return emtyIfNull(s).trim();
	}

	public static String[] trim(String s[])
	{
		String aux[] = new String[s.length];
		int i = 0;
		for(String x : s)
		{
			aux[i] = trim(s[i]);
			i++;
		}

		return aux;
	}

	public static String replicate(char c, int n)
	{
		String aux = "";
		for(int i = 0; i < n; i++)
		{
			aux += c;
		}

		return aux;
	}

	public static String lpad(String s, char c, int n)
	{
		return replicate(c, n - s.length()) + s;
	}
}
